package Srevice;
/*
    业务层：
    定义Exwife[] 数组保存对象
    1.响应ExwifeView的调用
    2.完成对前妻姐的各项信息的调用
 */

import domain.Exwife;
import utils.Utility;

public class ExwifeService {

    //属性
    private Exwife[] exwifes;
    private int exwifeNum = 1;  //记录前妻姐个数
    private int index = -1;     //记录要删除的数组下标
    private int updateId = -1;  //记录要更新信息的对象的数组下标

    //构造器
    public ExwifeService(int size){
        //new exwife
        exwifes = new Exwife[size];  //当创建前妻姐对象时指定数组大小
        //为了方便测试先初始化一个
        exwifes[0] = new Exwife("小美",18,"113","朝阳区",160,45);
    }

    //完成各个功能的方法

    //exit()方法用户确认是否要退出
    public boolean exit(boolean loop){
        char key = Utility.readConfirmSelection();
        if(key == 'Y'){
            loop = true;
        }
        return false;
    }

    //update()方法通过用户输入的名字对相应对象信息进行修改
    public void update(String updateName){
        for(int i=0;i<exwifeNum;i++){     //找到要修改的对象的数组下标
            if(exwifes[i].getName().equals(updateName)){
                updateId = i;
            }
        }
        //对相应信息进行修改，不需要修改的项直接回车
        System.out.println("姓名("+exwifes[updateId].getName()+"):");//修改姓名
        String newName = Utility.readString(10,"");
        System.out.println("年龄("+exwifes[updateId].getAge()+"):");//修改年龄
        int newAge = Utility.readInt(-1);
        System.out.println("电话("+exwifes[updateId].getPhone()+"):");//修改电话
        String newPhone = Utility.readString(13,"");
        System.out.println("地址("+exwifes[updateId].getAddress()+"):");//修改地址
        String newAddress = Utility.readString(10,"");
        System.out.println("身高("+exwifes[updateId].getHeight()+"):");//修改身高
        int newHeight = Utility.readInt();
        System.out.println("体重("+exwifes[updateId].getWeight()+"):");//修改体重
        int newWeight = Utility.readInt(-1);
    }

    //findByname()方法通过用户输入的名字搜索相应对象
    public Exwife findByname(String findName){
        for(int i=0;i<exwifeNum;i++){
            if(findName.equals(exwifes[i].getName())){
                return exwifes[i];
            }
        }
        return null;
    }

    //del()方法完成对前妻姐的删除操作
    public boolean del(String delname){
        for(int i=0;i<exwifeNum;i++){  //遍历找到要删除的对象数组下标
            if(exwifes[i].getName().equals(delname)){
                index = i;
            }
        }
        //完成删除操作
        for(int j=index;j<exwifeNum-1;j++){
            exwifes[j]=exwifes[j+1];
        }
        exwifes[exwifeNum-1] = null;
        exwifeNum--;
        return true;
    }

    //add()方法添加前妻姐到数组
    public boolean add(Exwife newexwife){
        if(exwifeNum ==exwifes.length){
            System.out.println("内存已满无法继续添加，请删除后继续添加！！");
            return false;
        }
        exwifes[exwifeNum] = newexwife;
        exwifeNum++;
        return true;
    }

    //list()方法，返回所有前妻姐信息
    public Exwife[] list(){
        return exwifes;
    }

}
